package service.impl;

import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class JSONEncoder {

	@SuppressWarnings("unchecked")
	public static JSONArray encodeWeekGraphData(Map<Integer, Double> averageByDay) {
		JSONArray jsonList = new JSONArray();
		for (Double studyTime : averageByDay.values()) {
			jsonList.add(studyTime);
		}
		return jsonList;
	}

	@SuppressWarnings("unchecked")
	public static JSONArray encodeMethodGraphData(Map<String, Double> percentageByMethod) {
		// Populate methodsData
		JSONObject methodsData = new JSONObject();
		JSONArray methodsDataList = new JSONArray();
		for (Double methodPrcnt : percentageByMethod.values()) {
			methodsDataList.add(methodPrcnt);
		}
		methodsData.put("methodPercents", methodsDataList);

		// Populate methodTypes
		JSONObject methodTypes = new JSONObject();
		JSONArray methodTypesList = new JSONArray();
		for (String method : percentageByMethod.keySet()) {
			methodTypesList.add(method + " (" + percentageByMethod.get(method) + "%)");
		}
		methodTypes.put("methodTypes", methodTypesList);

		// Add graph data and labels into one array
		JSONArray methodsAllData = new JSONArray();
		methodsAllData.add(methodsData);
		methodsAllData.add(methodTypes);

		return methodsAllData;
	}

}
